﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Singleton;

namespace vngoodlife.Models
{
    public class TagsModels
    {
        internal bool InsertTag(string newTagName, string adminName)
        {
            try
            {
                Tag tag = new Tag { TagName = newTagName, TagDate = DateTime.Now, AdminName = adminName };
                DataSingleton.Instance.GetLinQContext().Tags.InsertOnSubmit(tag);
                DataSingleton.Instance.GetLinQContext().Tags.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }

        /// <summary>
        /// Get tags list based on amount
        /// </summary>
        /// <param name="amountOfTags"></param>
        /// <returns></returns>
        internal IQueryable<Tag> GetTagsListBackend(string amountOfTags)
        {
            if (amountOfTags == "")
            {
                return DataSingleton.Instance.GetLinQContext().Tags.OrderByDescending(t => t.TagsID);
            }
            else
            {
                return DataSingleton.Instance.GetLinQContext().Tags.Take(Convert.ToInt32(amountOfTags)).OrderByDescending(t => t.TagsID);
            }
        }


        /// <summary>
        /// Get Tag
        /// </summary>
        /// <param name="tagtID"></param>
        /// <returns></returns>
        internal Tag GetTagDetails(int tagtID)
        {
            return DataSingleton.Instance.GetLinQContext().Tags.SingleOrDefault(t => t.TagsID == tagtID);
        }


        /// <summary>
        /// Update tag name
        /// </summary>
        /// <param name="tag"></param>
        internal void EditTag(Tag tag, string memberName)
        {
            try
            {
                Tag tempTag = DataSingleton.Instance.GetLinQContext().Tags.SingleOrDefault(t => t.TagsID == tag.TagsID);
                tempTag.TagName = tag.TagName;
                tempTag.TagDate = DateTime.Now;
                tempTag.Member = DataSingleton.Instance.GetLinQContext().Members.SingleOrDefault(m => m.MemberName == memberName);
                DataSingleton.Instance.GetLinQContext().Tags.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Create new tag
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="memberName"></param>
        internal void CreateTag(Tag tag, string memberName)
        {
            try
            {
                tag.TagDate = DateTime.Now;
                tag.AdminName = memberName;
                DataSingleton.Instance.GetLinQContext().Tags.InsertOnSubmit(tag);
                DataSingleton.Instance.GetLinQContext().Tags.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="tagID"></param>
        internal void DeleteTag(int tagID)
        {
            try
            {

                Tag tag = DataSingleton.Instance.GetLinQContext().Tags.SingleOrDefault(t => t.TagsID == tagID);
                DataSingleton.Instance.GetLinQContext().Tags.DeleteOnSubmit(tag);
                DataSingleton.Instance.GetLinQContext().Tags.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Get TagID based on Tag seourl
        /// </summary>
        /// <param name="tagsSeoUrl"></param>
        /// <returns></returns>
        internal Tag GetTagByTagSeoUrl(string tagsSeoUrl)
        {
            try
            {
                return DataSingleton.Instance.GetLinQContext().Tags.SingleOrDefault(u => u.TagSeoUrl == tagsSeoUrl);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}